// 批量处理器 - 控制并发避免内存问题
export class BatchProcessor {
    constructor(maxConcurrency = 3) {
        this.maxConcurrency = maxConcurrency;
        this.queue = [];
        this.activeCount = 0;
    }

    async process(tasks) {
        return new Promise((resolve) => {
            const results = new Array(tasks.length);
            let completed = 0;
            let index = 0;

            const runNext = () => {
                while (this.activeCount < this.maxConcurrency && index < tasks.length) {
                    const currentIndex = index++;
                    this.activeCount++;

                    tasks[currentIndex]()
                        .then(result => {
                            results[currentIndex] = { success: true, data: result };
                        })
                        .catch(error => {
                            results[currentIndex] = { success: false, error: error };
                        })
                        .finally(() => {
                            this.activeCount--;
                            completed++;

                            if (completed === tasks.length) {
                                resolve(results);
                            } else {
                                runNext();
                            }
                        });
                }
            };

            runNext();
        });
    }
}